//
//  KKChooseToolBar.swift
//  eyepetizer
//
//  Created by 柯平常 on 16/6/5.
//  Copyright © 2016年 stone. All rights reserved.
//

import UIKit
import SnapKit

private let baseTag = 1000
private let btnHeight = 40

class KKChooseToolBar: UIView {
    

    //定义属性 记录选中的按钮
    var currentBtn: UIButton?
    
    //定义闭包
    var selectedIndexCallBack: ((_ index: Int) -> ())?
    
    
    init() {
        super.init(frame: CGRect.zero)
        
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        let loginButton = addChildButtons(title: "登录", index: 0)
        let signUpButton = addChildButtons(title: "注册", index: 1)
        addSubview(lineView1)
        addSubview(lineView2)
        

        loginButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.snp.centerY)
            make.left.equalTo(self)
            make.width.equalTo(SCREEN_WIDTH/2)
            make.height.equalTo(btnHeight)
        }

        signUpButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.snp.centerY)
            make.right.equalTo(self)
            make.width.equalTo(SCREEN_WIDTH/2)
            make.height.equalTo(btnHeight)
        }
        
        lineView1.snp.makeConstraints { (make) in
            make.center.equalTo(self.center)
            make.width.equalTo(SCREEN_SCALE)
            make.height.equalTo(self)
        }
        
        lineView2.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.snp.bottom)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(SCREEN_SCALE)
        }

    }
    
    
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.darkGray
        view.alpha = 0.8
        return view
    }()
    
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.darkGray
        view.alpha = 0.8
        return view
    }()
    
    private func addChildButtons(title: String, index: Int)->UIButton {
        let btn = UIButton()
        //设置按钮的tag
        btn.tag = index + baseTag
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.titleLabel?.font = baseFont(size: 15)
        //设置按钮的点击事件
        btn.addTarget(self, action:#selector(btnDidClick(btn:)), for: .touchUpInside)
        btn.setTitleColor(UIColor(red: 13/255.0, green: 165/255.0, blue: 240/255.0, alpha: 1), for: .selected)
        addSubview(btn)
        if index == 0 {
            btn.isSelected = true
            //记录选中的按钮
            currentBtn = btn
        }
        return btn
    }
    
    @objc private func btnDidClick(btn: UIButton) {

        //取消之前按钮的选中状态
        currentBtn?.isSelected = false
        btn.isSelected = true
        
        currentBtn = btn
        
        selectedIndexCallBack!(btn.tag - baseTag)
    }
    
    
    func setSelectedBtn(section: Int) {
        //取消之前按钮的选中状态
        //根据section 获取对应的按钮
        //如果tag为 0 取到的是对象的自己
        let btntag = baseTag + section
        let btn = self.viewWithTag(btntag) as! UIButton
        currentBtn?.isSelected = false
        btn.isSelected = true
        currentBtn = btn
    }

}
